package com.skycong.hello77.activity.listview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import com.skycong.hello77.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author ruanmingcong
 * @date 2018/1/11 13:26
 */
public class TestListActivity extends Activity implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener {

    private Context context;
    private ListView listview;
    private List<DataVO> datas = new ArrayList<>();//数组适配器
    private TestListAdapter sim_adapter; //简单适配器
    private List<Map<String, Object>> dataList;//list数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_list);
        context = this;
        listview = (ListView) findViewById(R.id.list_view);

        setDatas();
        sim_adapter = new TestListAdapter(context, datas);
        //视图加载数据适配器
        listview.setAdapter((ListAdapter) sim_adapter);
        sim_adapter.notifyDataSetChanged();
        //加载事件监听器
//        listview.setOnItemClickListener(this);
//        listview.setOnScrollListener(this);
    }

   public void setDatas(){
       for (int i = 0; i < 3; i++) {
           List<String> strs = new ArrayList<>();
           for (int j = 0; j < 5; j++) {
               strs.add("第二层数据" + j);
           }
           datas.add(new DataVO("第一層數據" + i, strs));
       }
   }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //三种状态
        switch (scrollState) {
            case SCROLL_STATE_FLING:
                Log.i("lag", "用户在手指离开屏幕之前，用力滑了一下，视图由于惯性，依然向前滑动");

                break;
            case SCROLL_STATE_IDLE:
                Log.i("log", "视图停止滑动");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i("log", "手指没有离开屏幕，视图正在滑动");
                //下拉刷新
                // Map<String, Object> map = new HashMap<String, Object>();
                // map.put("img", R.mipmap.ic_launcher_round);
                // map.put("text", "新增商品");
                // dataList.add(map);
                //刷新页面，不然会报错adapter has changed but ListView did not receive a notification.
                // Make sure the content of your adapter is not modified from a background thread,
                // sim_adapter.notifyDataSetChanged();
                break;
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.i("log", "" + firstVisibleItem + "  " + visibleItemCount + " " + totalItemCount );
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        System.out.println("you click onItemClick");
        String atPosition = listview.getItemAtPosition(position) + "";
        Toast toast = Toast.makeText(this, "position" + position + "text" + atPosition, Toast.LENGTH_SHORT);
        //之定义Toast显示的位置//屏幕居中显示，X轴和Y轴偏移量都是0
        toast.setGravity(Gravity.CENTER, 0, 0);
        //显示Toast
        toast.show();
    }
}
